package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TraderAndStorageDao.GoodsMapper;
import com.hqyj.dao.TraderAndStorageDao.SuppliersMapper;
import com.hqyj.pojo.*;
import com.hqyj.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.jws.Oneway;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-10-03
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    private SuppliersMapper suppliersMapper;

    @Override
    public ResultVoH queryAllByStorageType(String storageType) {
        ResultVoH vo = new ResultVoH();
        List<Goods> goods = goodsMapper.queryAllByStorageType(storageType);
        if(goods.size()==1){
            vo.setInfo("该仓库没有商品");
            vo.setSuccess(false);
            return vo;
        }
        vo.setSuccess(true);
        vo.setData(goods);
        return vo;
    }

    @Override
    public ResponseResult findAll(Integer current, Integer size, String type, String suppliersName) {

        Page<Goods> goodsPage1 = new Page<>(current, size);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(type), Goods::getGoodsType,type);
        wrapper.eq(StringUtils.hasText(suppliersName), Goods::getGoodsSuppliersName,suppliersName);
        Page<Goods> goodsPage = page(goodsPage1, wrapper);
        List<Goods> goods = goodsPage.getRecords();
        //获取符合查询条件的总金额
        Integer totalMoney = goods.stream()
                .map(g -> g.getGoodsPrice())
                .reduce(0, (result, element) -> result + element);
        //获取符合查询条件的总数量
        long totalnum = goods.stream()
                .count();
        return new ResponseResult(AppHttpCodeEnum.SUCCESS,new GoodsVo(totalnum,totalMoney,goods));
    }

    @Override
    public ResponseResult getType() {
        List<Goods> goods = list();
        List<String> types = goods.stream()
                .map(g -> g.getGoodsType())
                .distinct()
                .collect(Collectors.toList());
        return new ResponseResult(AppHttpCodeEnum.SUCCESS,types);
    }

    @Override
    public ResponseResult getSuppliers() {
        List<Goods> goods = list();
        List<String> suppliers = goods.stream()
                .map(g -> g.getGoodsSuppliersName())
                .filter(s -> StringUtils.hasText(s))
                .distinct()
                .collect(Collectors.toList());
        return new ResponseResult(AppHttpCodeEnum.SUCCESS,suppliers);
    }

    @Override
    public ResponseResult insertGoods(Goods goods) {
        updateById(goods);
        return new ResponseResult(AppHttpCodeEnum.SUCCESS,null);
    }

    //商品列表
    @Override
    public ResultVoH goodsList(Goods goods) {
        ResultVoH resultVoH = new ResultVoH();
        //1.创建分页对象
        Page<Goods> page = new Page<>(goods.getPage(), goods.getRow());

        //创建条件构造器
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        //条件查询
        wrapper.like(
                goods.getGoodsName()!=null&&!goods.getGoodsName().equals(""),
                "goods_name",
                goods.getGoodsName());

        wrapper.like(
                goods.getGoodsType()!=null&&!goods.getGoodsType().equals(""),
                "goods_type",
                goods.getGoodsType());

        //2.查询分页
        Page<Goods> goodsPage = goodsMapper.selectPage(page, wrapper);
        if (goodsPage.getRecords().size() == 0){
            resultVoH.setInfo("没有符合条件的商品");
            return resultVoH;
        }

        //类型
        HashSet<String> goodsTypes = new HashSet<>();
        List<Goods> list = goodsMapper.selectList(null);
        for (Goods goods1 : list) {
            goodsTypes.add(goods1.getGoodsType());
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsList", goodsPage.getRecords());
        map.put("total", goodsPage.getTotal());
        map.put("totalPage", goodsPage.getPages());
        map.put("goodsTypes",goodsTypes);


        resultVoH.setData(map);
        resultVoH.setInfo("查询成功");
        resultVoH.setSuccess(true);

        return resultVoH;
    }

    @Override
    public ResultVoH goodsChange(Goods goods) {
        ResultVoH resultVoH = new ResultVoH();

        this.updateById(goods);

        resultVoH.setSuccess(true);
        resultVoH.setInfo("修改成功");
        return resultVoH;

    }

    //进货
    @Override
    public ResultVoH goodsAdd(Goods goods) {
        ResultVoH resultVoH = new ResultVoH();
        //1.创建分页对象
        Page<Goods> page = new Page<>(goods.getPage(), goods.getRow());

        //创建条件构造器
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        //条件查询
        wrapper.like(
                goods.getGoodsName()!=null&&!goods.getGoodsName().equals(""),
                "goods_name",
                goods.getGoodsName());

        wrapper.like(
                goods.getGoodsType()!=null&&!goods.getGoodsType().equals(""),
                "goods_type",
                goods.getGoodsType());

        //2.查询分页
        Page<Goods> goodsPage = goodsMapper.selectPage(page, wrapper);
        if (goodsPage.getRecords().size() == 0){
            resultVoH.setInfo("没有符合条件的商品");
            return resultVoH;
        }

        //类型
        HashSet<String> goodsTypes = new HashSet<>();
        List<Goods> list = goodsMapper.selectList(null);
        for (Goods goods1 : list) {
            goodsTypes.add(goods1.getGoodsType());
        }

        //查供货商
        List<String> goodsSuppliersNames = new ArrayList<>();
        List<Suppliers> suppliers = suppliersMapper.queryAll();
        for (Suppliers supplier : suppliers) {
            goodsSuppliersNames.add(supplier.getSuppliersName());
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsList", goodsPage.getRecords());
        map.put("total", goodsPage.getTotal());
        map.put("totalPage", goodsPage.getPages());
        map.put("goodsTypes",goodsTypes);
        map.put("goodsSuppliersNames",goodsSuppliersNames);

        resultVoH.setData(map);
        resultVoH.setInfo("查询成功");
        resultVoH.setSuccess(true);

        return resultVoH;
    }

    @Override
    public ResultVoH add(Goods goods) {
        QueryWrapper<Goods> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("goods_name", goods.getGoodsName());
        Goods one = goodsMapper.selectOne(wrapper1);
        //更新的数量
        one.setGoodsNums(one.getGoodsNums() + goods.getGoodsBuyNum());
        //更新的供货商
        one.setGoodsSuppliersName(goods.getGoodsSuppliersName());
        //更新的入库人
        one.setGoodsUserName(goods.getGoodsUserName());
        //更新的入库时间
        one.setGoodsJoinTime(new Date());

        //更新
        goodsMapper.updateById(one);

        ResultVoH resultVoH = new ResultVoH();
        resultVoH.setSuccess(true);
        resultVoH.setInfo("入库成功");
        return resultVoH;
    }
}
